﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer.DAL;

namespace BestBuy
{
    public partial class GuestView : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadCategory();
        }

        private void LoadCategory()
        {
            rpCategoryLevel1.DataSource = ProductCategoryDAL.GetCategories();
            rpCategoryLevel1.DataBind();
        }

        protected void rpCategoryLevel1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hid = (HiddenField)e.Item.FindControl("hrID1");
            Repeater rpChildrent2 = (Repeater)e.Item.FindControl("rpCategoryLevel2");
            if ((hid != null) && (rpChildrent2 != null))
            {
                var list = ProductCategoryDAL.GetChildrenOfCategory(int.Parse(hid.Value.ToString()));
                rpChildrent2.DataSource = list;
                rpChildrent2.DataBind();
                HyperLink link = (HyperLink)e.Item.FindControl("link1");
                if (link != null)
                {
                    link.Text = (string)DataBinder.Eval(e.Item.DataItem, "Name");
                    link.NavigateUrl = "";
                    if (list.Count == 0)
                    {
                        link.NavigateUrl = string.Format("~/PublicViews/SearchAuction.aspx?CategoryID={0}", hid.Value);
                        Panel pn = (Panel)e.Item.FindControl("pnIcon1");
                        if (pn != null)
                        {
                            pn.Visible = false;
                        }
                    }
                }
            }
        }

        protected void rpCategoryLevel2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hid = (HiddenField)e.Item.FindControl("hrID2");
            Repeater rpChildrent = (Repeater)e.Item.FindControl("rpCategoryLevel3");
            if ((hid != null) && (rpChildrent != null))
            {
                var list = ProductCategoryDAL.GetChildrenOfCategory(int.Parse(hid.Value.ToString()));
                rpChildrent.DataSource = list;
                rpChildrent.DataBind();
                HyperLink link = (HyperLink)e.Item.FindControl("link2");
                if (link != null)
                {
                    link.Text = (string)DataBinder.Eval(e.Item.DataItem, "Name");
                    link.NavigateUrl = "";
                    if (list.Count == 0)
                    {
                        link.NavigateUrl = string.Format("~/PublicViews/SearchAuction.aspx?CategoryID={0}", hid.Value);
                        Panel pn = (Panel)e.Item.FindControl("pnIcon2");
                        if (pn != null)
                        {
                            pn.Visible = false;
                        }
                    }
                }
            }
        }

        protected void rpCategoryLevel3_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink link = (HyperLink)e.Item.FindControl("link3");
            if (link != null)
            {
                int id = (int)DataBinder.Eval(e.Item.DataItem, "ID");
                link.Text = (string)DataBinder.Eval(e.Item.DataItem, "Name");
                link.NavigateUrl = string.Format("~/PublicViews/SearchAuction.aspx?CategoryID={0}", id);
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string str = string.Format("~/PublicViews/SearchAuction.aspx?NameSearch={0}", txtNameSearch.Text);
            Response.Redirect(str);
        }
    }
}